package jp.sourceforge.akjrcp.text_douga.item;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.ImageObject;
import jp.sourceforge.akjrcp.text_douga.rcp.TextDougaView.TextDougaExecute;

public class MultiStyledText extends AbstractItem{
	List<TextItem> textItems=new ArrayList<TextItem>();
	
	public void addTextItem(TextItem item){
		textItems.add(item);
	}
	
	public MultiStyledText(TextItem item){
		super(item);//just copy abstract
	}
	
	/*
	public Rectangle getDrawRect(DougaPreference preference,BufferedImage baseImage){
		
		
		
	}
	*/
	private boolean debug=false;
	
	public Dimension getDimension(DougaPreference preference,BufferedImage baseImage,int frame){
		
		BufferedImage img=new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2=(Graphics2D) img.getGraphics();
		ImageObject obj=new ImageObject(img);
		Point lastPosition=null;
		int dx=0; //TODO support layout;
		int dy=0;
		int my=0;
		int mx=0;
		for (int i = 0; i < textItems.size(); i++) {
			TextItem item=textItems.get(i);
			if(i==0){
				item.setX(dx);
				item.setY(dy);
			}else{
				if(lastPosition!=null){
					item.setX(dx);
					item.setY(lastPosition.y);
					item.setFirstOffset(lastPosition.x);
					item.setLastLineY(my);
				}else{
					item.setX(dx);
					item.setY(dy);
				}
			}
			try {
				item.writeFrame(preference, obj, frame);
			} catch (IOException e) {
				
				e.printStackTrace();
			}
			log.trace(item.getText()+":y="+item.getY());
			
			if(item.getLastDrawPosition()!=null){
			lastPosition=item.getLastDrawPosition();
			
			if(item.getMaxDrawedX()>mx){
				mx=item.getMaxDrawedX();
			}
			
			if(lastPosition.y+item.getLineHeight()>my){
				my=lastPosition.y+item.getLineHeight();
			}
			
			}else{
				log.warn("dimension:null");
			}
			TextDougaExecute.log.trace(item.getText()+":lastPosition="+lastPosition);
			item.resetMultiStyledSupport();
		}
		
		
		g2.dispose();
		Dimension dimension=new Dimension(mx,my);
		return dimension;
	}
	
	@Override
	public boolean writeFrame(DougaPreference preference,
			ImageObject baseImage, int frame) throws IOException {
		boolean writed=false;
		Point lastPosition=null;
		int dx=x; 
		int dy=y;
		int my=0;
		
		//check should write
		boolean written=false;
		if(duration==DougaSystem.FOREEVER){//maybe this is background image
			written=true;
		}
		double frameTime=(double)(frame-1)/preference.getFps();//to change start 0
		if(written==false){
			written=isInFrame(preference,frame);
			}
		
		if(written==false){
			return false;
		}
		
		
		Dimension d=getDimension(preference,baseImage.getImage(),frame);
		Rectangle drawRect=getDrawRect(preference,d);
		//System.out.println(d);
		//System.out.println(this.toString());
		//System.out.println(drawRect);
		dx=drawRect.x;
		dy=0;
		if(debug){
		log.warn("debug-mode:dimension:"+dx+","+dy+","+d);
		log.warn("debug-mode:rect:"+drawRect);
		baseImage.getImage().getGraphics().drawRect(dx,drawRect.y, d.width, d.height);//for debug dimension
		}
		
		
		//System.out.println("textSize:"+textItems.size());
		
		for (int i = 0; i < textItems.size(); i++) {
			TextItem item=textItems.get(i);
			//check
			if(item.getText()==null || item.getText().length()==0){
				log.warn("wrong text setted,maybe make problem");
			}
			if(i==0){
				item.setX(dx);
				item.setY(drawRect.y);
			}else{
				if(lastPosition!=null){
					//System.out.println(i+","+lastPosition);
					item.setX(dx);
					item.setY(lastPosition.y);
					item.setFirstOffset(lastPosition.x-dx);
					item.setLastLineY(my-dy);
				}else{
					log.info("time is not inside or:lastPosition=null:"+item.getText());
					
						item.setX(dx);
						item.setY(drawRect.y);
					}
			}
			log.trace(item.getText()+":y="+item.getY());
			boolean w=item.writeFrame(preference, baseImage, frame);
			if(w){
				writed=true;
			}
			
			lastPosition=item.getLastDrawPosition();
			if(lastPosition!=null){
			if(lastPosition.y+item.getLineHeight()>my){
				my=lastPosition.y+item.getLineHeight();
			}
			}else{
				log.warn("lastPosition:null");
			}
			TextDougaExecute.log.trace(item.getText()+":lastPosition="+lastPosition);
			item.resetMultiStyledSupport();
			
		}
		
		
		
		
		return writed;
	}
	
	public Rectangle getDrawRect(DougaPreference preference,Dimension dimension){
		
		
		
		
		int drawwidth=Math.max(dimension.width,width);
		int drawheight=Math.max(dimension.height,height);
		//draw width is text size or setted size.
		log.trace("text:"+drawwidth+","+drawheight);
		//Rectangle textRect=tmpG2.getFontMetrics(font).getStringBounds(text, context)
		int dw=drawwidth;
		int dh=drawheight;
		
		int dx=0;
		if(align==NONE){
			dx=x;
			dw=drawwidth;
		}else{
			if(align==LEFT){
				if(x==0 && width==0){//auto set
					dx=marginLeft;
				}else{
					dx=x;
				}
				dw=drawwidth;
			}else if(align==RIGHT){
				if(x==0 && width==0){
					dx=preference.getWidth()-dimension.width-marginRight;
				}else{
					dx=x+(drawwidth-dimension.width);
				}
				dw=drawwidth;
			}else if(align==CENTER){
				if(width==0){
					dx=marginRight+(preference.getWidth()-marginLeft-marginRight-dw)/2;
				}else{
					dx=x+(drawwidth-dimension.width)/2;
					
				}
				dw=drawwidth;
			}else{
				log.warn("invalid align:"+align);
			}
		}
		int dy=0;
		//here just make rectangle
		if(valign==NONE){
			dy=y;
			dh=drawheight;
		}else{
			if(valign==TOP){
				if(y==0 && height==0){//auto margin
					dy=y+marginTop;
				}else{
					dy=y;
				}
				dh=drawheight;
			}else if(valign==BOTTOM){
				if(y==0 && height==0){
					dy=preference.getHeight()-dimension.height-marginBottom;
				}else{
					dy=y+(drawheight-dimension.height);
					
				}
				dh=drawheight;
			}else if(valign==MIDDLE){
				if(height==0){
					dy=(preference.getHeight()-dimension.height-marginBottom)/2;
				}else{
					dy=y+(drawheight-dimension.height)/2;
					
				}
				dh=drawheight;
			}else{
				log.warn("invalid valign:"+valign);
			}
		}
		return new Rectangle(dx,dy,dw,dh);//just use dx,dy?
	}

}
